package com.xyq.system.controller.question;

import cn.hutool.core.collection.CollectionUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xyq.common.core.controller.BaseController;
import com.xyq.common.core.domain.Result;
import com.xyq.common.core.domain.TableDataInfo;
import com.xyq.system.domain.question.dto.QuestionAddDTO;
import com.xyq.system.domain.question.dto.QuestionEditDTO;
import com.xyq.system.domain.question.dto.QuestionSelectDTO;
import com.xyq.system.domain.question.vo.QuestionDetailVO;
import com.xyq.system.domain.question.vo.QuestionVO;
import com.xyq.system.service.question.QuestionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static com.xyq.common.core.enums.ResultCode.FAILED_PARAMS_VALIDATE;

@RestController
@RequestMapping("/question")
public class QuestionController extends BaseController {
    @Autowired
    private QuestionService questionService ;

    @GetMapping("/list")
    public TableDataInfo list(QuestionSelectDTO questionSelectDTO){

        List<QuestionVO> list = questionService.list(questionSelectDTO);
        return getTableList(list) ;
    }

    @PostMapping("/add")
    public Result add(@RequestBody @Validated QuestionAddDTO questionAddDTO){
        // 用BaseController里的toResult方法统一返回
        return toResult(questionService.add(questionAddDTO)) ;
    }

     @GetMapping("/detail")
    public Result<QuestionDetailVO> detail(Long questionId){
        // 如果参数不正确
        if(questionId == null){
            return Result.fail(FAILED_PARAMS_VALIDATE);
        }
         return  Result.success(questionService.detail(questionId));
     }

     @PutMapping("/edit")
     public Result edit(@RequestBody @Validated QuestionEditDTO questionEditDTO){

          return  toResult( questionService.edit(questionEditDTO));
     }

     @DeleteMapping("/delete")
    public Result delete( Long questionId ){
        return toResult(questionService.delete(questionId));
     }

}
